Skip to content

Conversation

@sli-tao
Copy link
Contributor

@sli-tao sli-tao commented Sep 30, 2025

Taoshi Pull Request

Description

  • Add value and quantity attributes to order

Related Issues (JIRA)

[Reference any related issues or tasks that this pull request addresses or closes.]

Checklist

  • I have tested my changes on testnet.
  • I have updated any necessary documentation.
  • I have added unit tests for my changes (if applicable).
  • If there are breaking changes for validators, I have (or will) notify the community in Discord of the release.

Reviewer Instructions

[Provide any specific instructions or areas you would like the reviewer to focus on.]

Definition of Done

  • Code has been reviewed.
  • All checks and tests pass.
  • Documentation is up to date.
  • Approved by at least one reviewer.

Checklist (for the reviewer)

  • Code follows project conventions.
  • Code is well-documented.
  • Changes are necessary and align with the project's goals.
  • No breaking changes introduced.

Optional: Deploy Notes

[Any instructions or notes related to deployment, if applicable.]

/cc @mention_reviewer

@sli-tao sli-tao changed the title Update slippage values Update Order Attributes Oct 2, 2025
@sli-tao sli-tao force-pushed the feat/slippage-v2 branch 2 times, most recently from 0889c21 to 594846c Compare October 9, 2025 06:01
@github-actions
Copy link

github-actions bot commented Oct 9, 2025

🤖 Claude AI Code Review

Last reviewed on: 14:23:47

Summary

This PR adds value and quantity attributes to orders, transitioning from a leverage-only model to support three position sizing methods (leverage, USD value, or base asset quantity). The changes span signal handling, order creation, position management, slippage calculation, and extensive test updates. This is a significant refactor affecting core trading logic.


✅ Strengths

  • Comprehensive test coverage updates across all test files
  • Backward compatibility maintained with optional field handling (data.get("leverage"))
  • Good documentation in sample signal request showing mutually exclusive options
  • Proper initialization with fallback logic for account_size
  • Extensive updates to position splitting and auto-sync tests

⚠️ Concerns

CRITICAL: Data Integrity & Migration Issues

  1. Missing Migration Strategy (Lines: vali_objects/position.py:51-58)

    • Adding net_value, net_quantity, and unrealized_pnl to Position model without migration path
    • Existing positions loaded from disk will have these fields set to 0.0 defaults
    • Impact: Historical position data may become inconsistent
    • Recommendation: Add migration script or version check to recalculate these fields for existing positions
  2. Breaking Change in Order Calculation (validator.py:1031-1063)

    def _add_order_to_existing_position(self, existing_position, trade_pair, signal_order_type: OrderType,
                                        quantity: float, order_time_ms: int, ...
    • Parameter changed from signal_leverage to quantity without version gating
    • Impact: Any validators on mixed versions may experience incompatibilities
    • Recommendation: Should be clearly documented as breaking change
  3. Account Size Validation (validator.py:1038-1043)

    if existing_position.account_size <= 0:
        bt.logging.warning(f"Invalid account_size...")
        existing_position.account_size = ValiConfig.MIN_CAPITAL
    • Silent mutation of critical position data
    • Issue: Modifying account_size on the fly could cause incorrect leverage calculations
    • Recommendation: Either fail fast or track this as a data quality issue

MAJOR: Logic & Correctness Issues

  1. Calculation Order Dependencies (validator.py:1031-1067)

    usd_base_price = self.live_price_fetcher.get_usd_base_conversion(...)
    value = (1 / usd_base_price) * (quantity * trade_pair.lot_size)
    leverage = value / existing_position.account_size
    • Complex calculation chain with no validation of intermediate values
    • Division by zero risk if usd_base_price is 0
    • Recommendation: Add validation and bounds checking
  2. Input Validation Gap (validator.py:1088-1108)

    fields_set = [x is not None for x in (leverage, value, quantity)]
    if sum(fields_set) != 1:
        raise ValueError("Exactly one of 'leverage', 'value', or 'quantity' must be set")
    • Good validation, but missing checks for:
      • Negative values (quantity, value should be positive)
      • Leverage bounds validation before conversion
      • NaN/Inf checks
    • Recommendation: Add comprehensive input sanitization
  3. Slippage Calculation Order Changed (validator.py:1064-1070)

    order.usd_base_rate = usd_base_price
    order.quote_usd_rate = self.live_price_fetcher.get_quote_usd_conversion(...)
    net_portfolio_leverage = self.position_manager.calculate_net_portfolio_leverage(miner_hotkey)
    order.slippage = PriceSlippageModel.calculate_slippage(order.bid, order.ask, order)
    • Removed refresh_features_daily() call
    • Changed calculate_slippage signature (removed account_size parameter)
    • Impact: Slippage calculations may differ from historical values
    • Recommendation: Document this behavioral change

MODERATE: Code Quality Issues

  1. Removed Parameter Without Refactoring (backtest_manager.py:106-107)

    def __init__(self, positions_at_t_f, start_time_ms, secrets, scoring_func,
                 use_slippage=None, ...
    • capital parameter removed but still referenced in call sites
    • Issue: This will break any external code using capital parameter
    • Recommendation: Deprecate properly or document the removal
  2. Test-Only Changes Leaking (Multiple test files)

    • All test files now require ValiBkpUtils.clear_directory() in setUp
    • This suggests the PositionManager has state pollution between tests
    • Root Cause: Likely a singleton pattern or shared state issue
    • Recommendation: Fix the underlying state management issue rather than working around it
  3. Magic Numbers (sample_signal_request.py:43-45)

    'leverage': 0.1,
    # 'value': 10_000,
    # 'quantity': 0.1,
    • Example values lack context about what they represent
    • Recommendation: Add comments explaining the position sizes

MINOR: Style & Documentation

  1. Inconsistent Newline Handling (Multiple files)

    • Adding newlines at EOF in some files but not others
    • Not functionally important but shows inconsistent formatting
  2. Grace Period Logic Removed (generate_request_minerstatistics.py:709-738)

    # TODO [remove on 2025-10-02] 70 day grace period --> reset upper bound
    • TODO removed along with grace period logic
    • Question: Was the grace period date reached, or was this premature removal?

💡 Suggestions

  1. Add Comprehensive Validation Helper

    @staticmethod
    def validate_order_inputs(leverage, value, quantity, account_size):
        """Validate order sizing inputs with comprehensive checks"""
        fields = {'leverage': leverage, 'value': value, 'quantity': quantity}
        non_none = {k: v for k, v in fields.items() if v is not None}
        
        if len(non_none) != 1:
            raise ValueError(f"Exactly one sizing method required, got: {list(non_none.keys())}")
        
        field_name, field_value = next(iter(non_none.items()))
        
        if not isinstance(field_value, (int, float)) or math.isnan(field_value) or math.isinf(field_value):
            raise ValueError(f"{field_name} must be a valid number")
        
        if field_name == 'leverage' and not (-10 <= field_value <= 10):
            raise ValueError(f"leverage must be between -10 and 10, got {field_value}")
        
        if field_name in ['value', 'quantity'] and field_value <= 0:
            raise ValueError(f"{field_name} must be positive, got {field_value}")
        
        return field_name, field_value
  2. Add Migration Script

    • Create migrations/migrate_v7_0_2_to_v7_0_3.py to handle:
      • Recalculating net_value and net_quantity from existing orders
      • Populating unrealized_pnl based on current positions
      • Validating data integrity
  3. Add Integration Test

    • Test the three different order input methods end-to-end
    • Verify they produce equivalent positions when properly sized
    • Test edge cases (very small/large positions)
  4. Document Conversion Formulas
    Add to docstring or separate docs:

    Conversion formulas:
    - leverage -> quantity: quantity = (leverage * account_size * usd_base_rate) / lot_size
    - value -> quantity: quantity = (value * usd_base_rate) / lot_size
    - quantity -> value: value = (quantity * lot_size) / usd_base_rate
    - value -> leverage: leverage = value / account_size
    
  5. Add Monitoring/Logging

    • Log which sizing method is used for each order
    • Track conversion accuracy (compare calculated vs actual execution)
    • Alert on unusual conversions (e.g., very high leverage from value input)
  6. Consider API Versioning

    • This is a significant change to the signal API
    • Consider adding version parameter to signals for better compatibility tracking

🔒 Security Notes

  1. Input Validation Required (MEDIUM SEVERITY)

    • The parse_order_quantity method trusts input values without bounds checking
    • Malicious miners could send extreme values to cause:
      • Integer overflow in calculations
      • Division by zero
      • Memory exhaustion (very large position calculations)
    • Mitigation: Add strict bounds validation before calculations
  2. Account Size Manipulation (LOW SEVERITY)

    • Warning-only handling of invalid account_size could be exploited
    • If miners can force account_size to MIN_CAPITAL, they might game leverage limits
    • Mitigation: Consider failing the order or marking the position as invalid
  3. Race Condition Potential (LOW SEVERITY)

    • Multiple receive_signal calls could race on position updates
    • The lock comment says "Multiple threads can run receive_signal at once"
    • Note: Existing locking should handle this, but verify with the new calculation flow
  4. API Key Validation (sample_signal_request.py:46)

    • Example shows placeholder 'api_key': 'xxxx'
    • Ensure production deployment has proper API key validation
    • Recommendation: Add rate limiting per API key

🎯 Action Items (Priority Order)

Must Fix Before Merge:

  1. Add input validation for negative/NaN/Inf values in parse_order_quantity
  2. Document the breaking changes in PR description
  3. Add bounds checking before division operations
  4. Create migration plan for existing position data

Should Fix Before Merge:
5. Add integration test for all three order sizing methods
6. Document conversion formulas in code comments
7. Fix account_size validation to fail fast rather than warn-and-mutate
8. Add version check or feature flag for slippage changes

Nice to Have:
9. Add monitoring/logging for order sizing method usage
10. Consider API versioning for signal endpoint
11. Add performance benchmarks for new calculation path
12. Investigate PositionManager state pollution in tests


📊 Test Coverage Analysis

Positive:

  • Extensive test updates showing thoroughness
  • Edge cases covered (zero positions, closed positions, etc.)
  • Multiple trade pairs tested

Gaps:

  • No test for parse_order_quantity with invalid inputs
  • No test for usd_base_price = 0 edge case
  • No test verifying equivalence of the three sizing methods
  • No performance/load test for conversion calculations

Overall Assessment

This is a substantial and well-intentioned change that improves flexibility in position sizing. However, it introduces breaking changes and potential data integrity issues that need careful handling. The code quality is generally good, but the migration path and input validation need strengthening before production deployment.

Recommendation: Request Changes - Address critical validation and migration concerns before merging.

@sli-tao sli-tao force-pushed the feat/slippage-v2 branch 3 times, most recently from 0be1bea to e617bca Compare October 28, 2025 07:50
@sli-tao sli-tao mentioned this pull request Nov 3, 2025
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants